package com.bilibili.task;



import com.bilibili.dto.VideoToJedis;
import com.bilibili.mapper.VideoMapper;
import com.bilibili.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import java.util.List;

import static com.bilibili.constant.BusinessConstant.REDIS_COMPLETE_VIDEO_KEY;

/**
 * 在服务启动的时候如果在Redis中没有点击量数据，则将点击量数据从MySQL中同步到Redis中
 */
@Component
public class MySqlClickCountToRedis {
    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private RedisOperations redisOperations;

    @PostConstruct
    public void execute() {
        //判断redis中有无点击量数据对应的key
        boolean exists = redisOperations.exists(REDIS_COMPLETE_VIDEO_KEY);

        //如果没有，则将MySQL中的点击量数据写入Redis中
        if (!exists) {
            System.out.println("将MySQL中的选项同步到Redis中start...");
            List<VideoToJedis> allVideoToJedis = videoMapper.getAllVideoToJedis();
            redisOperations.set(REDIS_COMPLETE_VIDEO_KEY,allVideoToJedis);
            System.out.println("将MySQL中的选项数据同步到Redis中end...");
        }
    }
}
